/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.core;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Policy
{
	private static ResourceBundle bundle = null;


	/**
	  * Private constructor for utility class.
	  */
	private Policy()
	{
	}
	
	/**
	  * Creates a NLS catalog for the given locale.
	  */
	public static void localize(String bundleName) 
	{
		bundle = ResourceBundle.getBundle(bundleName);
	}
	
	/**
	  * Lookup the message with the given ID in this catalog and bind its
	  * substitution locations with the given string.
	  */
	public static String bind(String id, String binding) 
	{
		return bind(id, new String[] { binding });
	}
	
	/**
	  * Lookup the message with the given ID in this catalog and bind its
	  * substitution locations with the given strings.
	  */
	public static String bind(String id, String binding1, String binding2) 
	{
		return bind(id, new String[] { binding1, binding2 });
	}
	
	/**
	  * Gets a string from the resource bundle. We don't want to crash because of a missing String.
	  * Returns the key if not found.
	  */
	public static String bind(String key) 
	{
		try 
		{
			return bundle.getString(key);
		} 
		catch (MissingResourceException e) 
		{
			return key;
		} 
		catch (NullPointerException e) 
		{
			return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	
	/**
	  * Gets a string from the resource bundle and binds it with the given arguments. If the key is 
	  * not found, return the key.
	  */
	public static String bind(String key, Object[] args) 
	{
		try 
		{
			return MessageFormat.format(bind(key), args);
		} 
		catch (MissingResourceException e) 
		{
			return key;
		} 
		catch (NullPointerException e) 
		{
			return "!" + key + "!"; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}	
	
	public static IProgressMonitor monitorFor(IProgressMonitor monitor)
	{
		if (monitor == null)
			return new NullProgressMonitor();
			
		return monitor;
	}
	
	public static void assertTrue(boolean condition)
	{
		if (condition == false)
			throw new AssertionError("Assertion failed."); //$NON-NLS-1$
	}

	public static void assertInstanceOf(Object object, Class clazz)
	{
		if (!clazz.isInstance(object))
			throw new AssertionError("Assertion failed: object has to be instance of " + clazz); //$NON-NLS-1$
	}
	
	public static void assertNotEmpty(String text)
	{
		if (text.length() == 0)
			throw new AssertionError("Assertion failed: String may not be empty."); //$NON-NLS-1$
	}
	
	public static void assertNotNull(Object object)
	{
		if (object == null)
			throw new AssertionError("Assertion failed: object may not be null."); //$NON-NLS-1$
	}

	public static void assertNull(Object object)
	{
		if (object != null)
			throw new AssertionError("Assertion failed: object has to be null."); //$NON-NLS-1$
	}
}
